# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_flavor=lts
pkgname=linux-${_flavor}
pkgver=5.4.58
case $pkgver in
	*.*.*)	_kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=0
pkgdesc="Linux lts kernel"
url="https://www.kernel.org"
depends="mkinitfs"
_depends_dev="perl gmp-dev elfutils-dev bash flex bison"
makedepends="$_depends_dev sed installkernel bc linux-headers linux-firmware-any openssl-dev
	diffutils"
options="!strip"
_config=${config:-config-lts.${CARCH}}
install=
source="https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kernver.tar.xz
	0002-powerpc-config-defang-gcc-check-for-stack-protector-.patch

	0001-arm64-dts-lx2160a-add-mdio-device-tree-nodes.patch
	0001-arm64-dts-lx2160a-add-lx2160acex7-device-tree-build.patch
	0002-arm64-dts-lx2160a-add-lx2160acex7-device-tree.patch
	0005-arm64-dts-lx2160a-cex7-add-ltc3882-support.patch
	0006-arm64-dts-lx2160a-cex7-add-on-module-eeproms.patch
	0007-pci-hotplug-declare-IDT-bridge-as-hotpluggabl-bridge.patch
	0008-pci-spr2803-quirk-to-fix-class-ID.patch

	config-lts.aarch64
	config-lts.armv7
	config-lts.x86
	config-lts.x86_64
	config-lts.ppc64le
	config-lts.s390x
	config-lts.mips64
	config-lts.mips

	config-virt.aarch64
	config-virt.armv7
	config-virt.ppc64le
	config-virt.x86
	config-virt.x86_64
	"
subpackages="$pkgname-dev:_dev:$CBUILD_ARCH"
_flavors=
for _i in $source; do
	case $_i in
	config-*.$CARCH)
		_f=${_i%.$CARCH}
		_f=${_f#config-}
		_flavors="$_flavors ${_f}"
		if [ "linux-$_f" != "$pkgname" ]; then
			subpackages="$subpackages linux-${_f}::$CBUILD_ARCH linux-${_f}-dev:_dev:$CBUILD_ARCH"
		fi
		;;
	esac
done

if [ "${pkgver%.0}" = "$pkgver" ]; then
	source="$source
	https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz"
fi
arch="all !armhf"
license="GPL-2.0"

_carch=${CARCH}
case "$_carch" in
aarch64*) _carch="arm64" ;;
arm*) _carch="arm" ;;
mips*) _carch="mips" ;;
ppc*) _carch="powerpc" ;;
s390*) _carch="s390" ;;
esac

prepare() {
	local _patch_failed=
	cd "$srcdir"/linux-$_kernver
	if [ "$_kernver" != "$pkgver" ]; then
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N
	fi

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# remove localversion from patch if any
	rm -f localversion*
	oldconfig
}

oldconfig() {
	for i in $_flavors; do
		local _config=config-$i.${CARCH}
		local _builddir="$srcdir"/build-$i.$CARCH
		mkdir -p "$_builddir"
		echo "-$pkgrel-$i" > "$_builddir"/localversion-alpine \
			|| return 1

		cp "$srcdir"/$_config "$_builddir"/.config
		make -C "$srcdir"/linux-$_kernver \
			O="$_builddir" \
			ARCH="$_carch" \
			listnewconfig oldconfig
	done
}

build() {
	unset LDFLAGS
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"
	for i in $_flavors; do
		cd "$srcdir"/build-$i.$CARCH
		make ARCH="$_carch" CC="${CC:-gcc}" \
			KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	done
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	local _abi_release=${pkgver}-${pkgrel}-${_buildflavor}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	cd "$srcdir"/build-$_buildflavor.$CARCH
	# modules_install seems to regenerate a defect Modules.symvers on s390x. Work
	# around it by backing it up and restore it after modules_install
	cp Module.symvers Module.symvers.backup

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	local _install
	case "$CARCH" in
		arm*|aarch64) _install="zinstall dtbs_install";;
		*) _install=install;;
	esac

	make -j1 modules_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$_outdir/boot/dtbs-$_flavor"

	cp Module.symvers.backup Module.symvers

	rm -f "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf "$_outdir"/lib/firmware

	install -D include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release
}

# main flavor installs in $pkgdir
package() {
	depends="$depends linux-firmware-any"

	_package lts "$pkgdir"
}

# subflavors install in $subpkgdir
virt() {
	_package virt "$subpkgdir"
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local _abi_release=${pkgver}-${pkgrel}-$_flavor
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/config-$_flavor.${CARCH} "$dir"/.config
	echo "-$pkgrel-$_flavor" > "$dir"/localversion-alpine

	make -j1 -C "$srcdir"/linux-$_kernver O="$dir" ARCH="$_carch" \
		syncconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$srcdir"/linux-$_kernver
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' -o -name 'Platform' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"

	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build-$_flavor.$CARCH/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="9f60f77e8ab972b9438ac648bed17551c8491d6585a5e85f694b2eaa4c623fbc61eb18419b2656b6795eac5deec0edaa04547fc6723fbda52256bd7f3486898f  linux-5.4.tar.xz
d19365fe94431008768c96a2c88955652f70b6df6677457ee55ee95246a64fdd2c6fed9b3bef37c29075178294a7fc91f148ead636382530ebfa822be4ad8c2f  0002-powerpc-config-defang-gcc-check-for-stack-protector-.patch
cfcd03668a80944706c66f89055f641c972c1839bf33123c19d63681d3d3958c8a0d4ab0679af9b921d0e9fd0c25445b8c417a21d1519e3d196780a0d5bd1520  0001-arm64-dts-lx2160a-add-mdio-device-tree-nodes.patch
1c87556c43b20c70e40d628624f0043a7dea1c3e66be26e15f594a1aed449a11f214795617d25309e1c1d5af92ec644222c4e68be652be043bdda80af0c0ece2  0001-arm64-dts-lx2160a-add-lx2160acex7-device-tree-build.patch
e75ab07e3d2d0d3fd085fb4132b3fee1650ae3bf1b6eaefe1f92b90071547a4f3ceef35ad4ce0f9345e78fac2de1697f6db3de1a526e730262be7ff299bd0038  0002-arm64-dts-lx2160a-add-lx2160acex7-device-tree.patch
d27bf4b8169ac047f60beae326da8f5e4d4f887c893cb4dd97f87d0c363d02e00704bf07d2026d9fd622b612cd9abbebfe4c5adfb7c033d73939b959e9652add  0005-arm64-dts-lx2160a-cex7-add-ltc3882-support.patch
926cb8f9a15f82bb90db962c9b7f7cbb3dbb754d3b1a0d7d12c0593ea60de8f07744ccd7c86e0e43daf1eab3392cc8624786680e1244fa96982e676a908ab88d  0006-arm64-dts-lx2160a-cex7-add-on-module-eeproms.patch
ca5aafac37e0b5f3fcbaf801e12f98beb58ffaf1d8c88f76caff22b059831869b4094e7fdcb6d6860422d6b2d036e072caff460e1feb84bd04d10740ad56265b  0007-pci-hotplug-declare-IDT-bridge-as-hotpluggabl-bridge.patch
cbe85cf34e8420c91d2276c2d2aa0ab5023af68e57a1fa613f073f16a76766c67f585eda71c28f232bd0625e0dc8275a9eddc95f49409205dc0dbcc28c9fac1c  0008-pci-spr2803-quirk-to-fix-class-ID.patch
4327ec22203cb3b84d67a560077007dbf91af17c0369ae4cde7e2de30ce8590751eb54aa8de41042124d09e86f2f7af6fddfaf0aebc868b743a386f072d3c790  config-lts.aarch64
d55d697addc87fb430e711d54d237780b06eab34ece004fbb1c23649e00f637cc707cc370ec352f4d6edaa5ead5330f849d625c9b0470d10cc0e4a964a2135d0  config-lts.armv7
4aa2c5061663debc0a6b14c0ade9c980d169b937d9a1eeb182ff2cdcabceabb4c139c1a9b8ed6cc7df888ef13375c04e32946ea0afb49d038236ec07885b62e7  config-lts.x86
631cc00596f0e9d90a602b98040211a375279c38ec577ba4954bde37114e5518b374a59e242be3a8d311bd2aa2e0141fa3d1aea9e5dd6d1abc9885067569898a  config-lts.x86_64
38ac135a78ef662b8f785aebe9feecb9f3aa333f0eb7984891a8c2db0f4d5744b54cb9fc9b820ea5a7e71e0ee52891286eeb53e9056ec8833358539b14451229  config-lts.ppc64le
a6c82d78d124d777297533269a376a996dea3a60b75e50fc94ceb852aa29f5175ca08b3f233db7f7c423ca1f085b05a038a005e07f6dc479542246476126e315  config-lts.s390x
1bb04a6621a8581c782f793f6081fa390fc9167e5b4e0e0619d0a48b0137706065c543ebc4a7de1c2b60bff9282cd94f9164834a8f9a37a09387dc629f5962a4  config-lts.mips64
4ac2ded62bc4ee683ccd308c2a17fed25ddad44e2c59f505b4c2f65c925172fc2d0412ef51a707937122492aa0de1c3b70fe3a4f613732cbf883358c49320574  config-lts.mips
c1d75ca3dbb0872e39ff4d95698f9e1c5c1727178a62b39e8a1563de85ede9a179d91c1aa94e7f238d461b91fa45d3b7bc6556a4c7ff520f41fa09a30c9025a0  config-virt.aarch64
185589999cf5215aa614a1a5c4eab9c6763de6b9525ba66e109c458079fc61b51c5712d2d462911e5b1f17ba9ec5a25800dc32139d16fa7646ccfc6663132c20  config-virt.armv7
f02115e419c5e2d1091b578bcfd39c9c910e7cc6cfe0ca5d17c99cc04d289c8efa57ebacd99f439288687b873a1da03806f9f2abcf8154ff467b42f8886b6d26  config-virt.ppc64le
29a1078ea69484d84f124b96fe13090e5079b5f841c46cb014a024a16568cb559a01948ce122d853de5b71f6acf6c0661f2a4a82b8ff775884047a632f629054  config-virt.x86
70369ebc0c44cf84222126e1536de5cc484c594c74b6a89567871d1067f4843d7654f2d8c4250217dbd9719540c9dddfdd483587c65bb65ff76998eb0b8cfa95  config-virt.x86_64
9b821aa688f14c2336ce53c95e5302538439ad7a6e440010fa1a033d760e1adfdfec3d4aa3ad0a65d366ed051474cd20b0b91a076d6932f1b77c2e6ec47eb8f1  patch-5.4.58.xz"
